/**
 * Created by zyy on 15/7/7.
 * zhangyuyu@superjia.com
 * collect:{
 *     user_id:'', 用户ID
 *     title: '', 文章标题
 *     text: '', 推荐语
 *     url: '', 文章链接
 *     createTime:'', 创建时间
 * }
 */


var router = require('koa-router')({
    prefix: '/collects'
});

var wrap = require('co-monk')
var parse = require('co-body')

var filter = require('../common/filter')
var utils = require('../common/utils')
var render = require('../common/render')

var db = require('../db')

var collectDao = wrap(db.get('collects'))
var userDao = wrap(db.get('user'))

router.get('/list', function * (next) {
	var list = yield collectDao.find({});
    utils.success(this, list);
})

router.post('/add', filter.login,function * (next) {
	  var body = yield parse(this);
	  var user = this.locals._user;

    var cloo = {
        user_id: user._id,
        title: body.title, 
        text: body.text, 
        url: body.url,
        createTime: Date.now()
    }

	  yield collectDao.insert(cloo);
    utils.success(this, cloo)
})

module.exports = router